﻿package divinity.fx.sfx
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	import flash.media.Sound;
	import flash.media.SoundChannel;
	import flash.media.SoundMixer;
	import flash.media.SoundTransform;
	import flash.net.URLRequest;
	
	public class SoundFX extends EventDispatcher
	{
		private var _globalVolume:Number=.8;
		private var _channels:Array=[];
		private var _ch:int=0;
		private var _soundBank:*;
		private var _world:Object;
		private var _makingSound:Boolean=false;
		public function SoundFX(bank:*,target:*=null)
		{
			super(target);
			_soundBank=bank;
			_world=target;
		
		}
		public function playSong(sng:String,vol:Number=1)
		{
			
			var req:URLRequest= new URLRequest(sng);
			var sound:Sound=new Sound();
			//sound.addEventListener(Event.ID3,showSongData);
			//sound.addEventListener(ProgressEvent.PROGRESS,loadProgress);
			var transform:SoundTransform = new SoundTransform(_globalVolume*vol);		
			sound.load(req);
			_channels[_ch]=sound.play();	
			_channels[_ch].soundTransform = transform;
			_channels[_ch].addEventListener(Event.SOUND_COMPLETE,songComplete);			
				
		}
		public function fx(snd:String,vol:Number=1,loop:int=0):void
		{
			try{
				_makingSound=true;
				//trace("[SOUND FX] "+snd);
				var soundFx:Class=_soundBank.loaderInfo.applicationDomain.getDefinition(snd)  as  Class;			
				var newFx:Sound=new soundFx();
				var transform:SoundTransform = new SoundTransform((_globalVolume*.3)*vol);
				var chan=newFx.play(0,loop);	
				chan.soundTransform = transform;
				_makingSound=false
			}
			catch(e:Error)
			{
				trace("Hey hey ... hold your horses boy");	
			}
			
			
		}
		private function songComplete(e:Event):void
		{
			//trace("song complete");
		}
		public function stopAllPlay()
		{
			SoundMixer.stopAll();
		}
		public function stopPlay()
		{
				
		}
	}
}